package com.gosophia.conference.action.summary;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.conferenceManagement.notification.ws.NotificationWebService;
import com.gosophia.conferenceManagement.scheme.ws.ConferenceSchemeWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.summary.dto.SummaryJobDataDTO;
import com.gosophia.conferenceManagement.summary.ws.SummaryWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

/**
 * 跳转到审核总结页面或取回修改页面
 * @author mayue
 *
 */
@Scope("prototype")
@Controller("ToApprovalSummaryPageAction")
public class ToApprovalSummaryPageAction extends BaseSupportAction{

    private static final long serialVersionUID = 1L;
    
    @Autowired
    private UserManagementWebService userManagementWebService;
    
    @Autowired
    private SummaryWebService summaryWebService;
    
    @Autowired
    private TaskManagementWebService taskManagementWebService;
    
    @Autowired
    private ConferenceSchemeWebService conferenceSchemeWebService;
    
    @Autowired
    private NotificationWebService notificationWebService;
    
    @Autowired
    private ConferenceQueryWebService conferenceQueryWebService;
    
    @Autowired
    private JudgeCurrentUndertaker judgeCurrentUndertaker;
    
    
    private SummaryJobDataDTO summaryJobDataDTO;
    
    private String nextApprovalListString;
    
    private String onlinePath;
    
    private String onlineVersion;
    
    private Long jobId;
    
    private String jobType;
    
    //总结审核历史记录
    private List<JobDTO> approvalHistory;
    //会议方案及审批信息
    private List<JobDTO> schemeHistoryRecord;
    //通知历史记录
    private List<JobDTO> notificationHistoryRecord;
    //预通知审批历史记录
    private List<JobDTO> notificationPreHistoryRecord;
    //任务的信息
    private ConferenceStartingDTO conferenceStartingDTO;

    @Override
    public String _execute() throws Exception {
        
        //判断当前用户和job承办人是否匹配,如果不匹配转到异常页面
        judgeCurrentUndertaker.judgeOperationAuthority(jobId);
        
        //下一审批人列表json串
        List<UserInfoDTO> allUsers = userManagementWebService.findAllUserInfoHasAccount();
        
        if(allUsers!=null){
            nextApprovalListString = JSONArray.fromObject(allUsers).toString().replace("\"", "\\\"");
        }
        
        
        if(jobId!=null){
          //要审核的jobData
            summaryJobDataDTO = summaryWebService.findPreSummaryJobDataByJobDataId(jobId);
            //显示在线文档
            if(summaryJobDataDTO!=null && summaryJobDataDTO.getOnlineDocumentDTO()!=null){
                onlinePath = summaryJobDataDTO.getOnlineDocumentDTO().getDocumentPath();
                onlineVersion= summaryJobDataDTO.getOnlineDocumentDTO().getDocumentVersion();
            }
            //获取jobType
            JobDTO jobDTO = taskManagementWebService.findJobDetailByJobId(jobId);
            if(jobDTO!=null){
                jobType=jobDTO.getJobType();
            }
            
            //会议总结审核历史记录
            approvalHistory = summaryWebService.findSummaryHistoryByJobId(jobId);
            //会议方案及审批信息
            schemeHistoryRecord=conferenceSchemeWebService.findFinishedSchemeApprovalHistoryRecordByJobId(jobId);
            //通知审批历史记录
            notificationHistoryRecord=notificationWebService.findFinishedNotificationApprovalHistoryRecordByJobId(jobId, "1");
            //预通知审批历史记录
            notificationPreHistoryRecord=notificationWebService.findFinishedNotificationApprovalHistoryRecordByJobId(jobId,"0");
            //任务的信息
            conferenceStartingDTO = conferenceQueryWebService.findConferenceMasterByJobId(jobId);
            
        }
        
        
        if("总结审批".equals(jobType)){//当前JobType为"总结审批"，跳转到审批页面
            return "summaryApproval";
        }
        return SUCCESS;
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public SummaryJobDataDTO getSummaryJobDataDTO() {
        return summaryJobDataDTO;
    }

    public void setSummaryJobDataDTO(SummaryJobDataDTO summaryJobDataDTO) {
        this.summaryJobDataDTO = summaryJobDataDTO;
    }

    public List<JobDTO> getApprovalHistory() {
        return approvalHistory;
    }

    public void setApprovalHistory(List<JobDTO> approvalHistory) {
        this.approvalHistory = approvalHistory;
    }

    public String getNextApprovalListString() {
        return nextApprovalListString;
    }

    public void setNextApprovalListString(String nextApprovalListString) {
        this.nextApprovalListString = nextApprovalListString;
    }

    public String getOnlinePath() {
        return onlinePath;
    }

    public void setOnlinePath(String onlinePath) {
        this.onlinePath = onlinePath;
    }

    public String getOnlineVersion() {
        return onlineVersion;
    }

    public void setOnlineVersion(String onlineVersion) {
        this.onlineVersion = onlineVersion;
    }

    public String getJobType() {
        return jobType;
    }

    public void setJobType(String jobType) {
        this.jobType = jobType;
    }

    public List<JobDTO> getSchemeHistoryRecord() {
        return schemeHistoryRecord;
    }

    public void setSchemeHistoryRecord(List<JobDTO> schemeHistoryRecord) {
        this.schemeHistoryRecord = schemeHistoryRecord;
    }

    public List<JobDTO> getNotificationHistoryRecord() {
        return notificationHistoryRecord;
    }

    public void setNotificationHistoryRecord(List<JobDTO> notificationHistoryRecord) {
        this.notificationHistoryRecord = notificationHistoryRecord;
    }

    public List<JobDTO> getNotificationPreHistoryRecord() {
        return notificationPreHistoryRecord;
    }

    public void setNotificationPreHistoryRecord(
            List<JobDTO> notificationPreHistoryRecord) {
        this.notificationPreHistoryRecord = notificationPreHistoryRecord;
    }

    public ConferenceStartingDTO getConferenceStartingDTO() {
        return conferenceStartingDTO;
    }

    public void setConferenceStartingDTO(ConferenceStartingDTO conferenceStartingDTO) {
        this.conferenceStartingDTO = conferenceStartingDTO;
    }
    
    
}
